package com.sysware.jbrowser;

import java.awt.Container;
import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;

/**
 * Hello world!
 *
 */
public class JFrameBrowser extends JFrame implements HyperlinkListener {
	private static final long serialVersionUID = 1L;

	public JFrameBrowser() {
		Container c = getContentPane();
		setSize(539, 412);
		// setTitle("QQ登录界面");
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		JEditorPane pane = new JEditorPane();
		pane.setContentType("text/html");
		pane.setEditable(false);
		pane.addHyperlinkListener(this);
		String urlOpen = "https://www.baidu.com";
		try {
			pane.setPage(urlOpen);

		} catch (IOException e) {
			e.printStackTrace();
		}
		// pane.setText("ss");
		pane.setBounds(0, 0, 539, 199);
		c.add(pane);
		setVisible(true);
	}

	public static void main(String[] args) {
		new JFrameBrowser();
	}

	public void hyperlinkUpdate(HyperlinkEvent e) {
		if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
			JEditorPane pane = (JEditorPane) e.getSource();
			if (e instanceof HTMLFrameHyperlinkEvent) {
				HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent) e;
				HTMLDocument doc = (HTMLDocument) pane.getDocument();
				doc.processHTMLFrameHyperlinkEvent(evt);
			} else {
				try {
					pane.setPage(e.getURL());
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}

	}
}
